package moriarty.gwt.client;

import java.util.Date;

import moriarty.gwt.shared.Submission;
import moriarty.gwt.shared.Utils;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.TextBoxBase;

public class SubmissionWidget extends Composite{
    /** Keep track of the currently focused widget */
    private int tabIndex = 0;
    private TextBoxBase[] userInputBoxes;
    private TextBox selector = new TextBox();
    private TextBox artist = new TextBox();
    private TextBox title = new TextBox();
    private TextArea comment = new TextArea();
    private TextBox link = new TextBox();
    private TextArea sotdSubscribers = new TextArea();
    
    
    KeyDownHandler keyDownHandler = new KeyDownHandler() {
		
		@Override
		public void onKeyDown(KeyDownEvent event) {

			// TAB key pressed
			if (event.getNativeKeyCode() == 9) {
				event.preventDefault();
			    event.stopPropagation();

			    // Increment to next tabbed widget.  There are five total,
			    // so loop back to the first if we've reached the last one.
			    userInputBoxes[tabIndex].setFocus(false);
			    tabIndex++;
			    tabIndex %= 6;
			    userInputBoxes[tabIndex].setFocus(true);
			    userInputBoxes[tabIndex].selectAll();
			}
		}
	};

	FocusHandler focusHandler = new FocusHandler() {
		
		@Override
		public void onFocus(FocusEvent event) {
			((TextBoxBase)event.getSource()).selectAll();
			
		}
	};
    
    public SubmissionWidget(){
        userInputBoxes = new TextBoxBase[6];
        String labelWidth = "75px";
        
        //Text Box for the selector
        final Label selectorLabel = new Label("Your Name:");
        selectorLabel.setWidth(labelWidth);
        selectorLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
        
        selector.setText("[Your Name]");
        selector.setWidth("400px");
        userInputBoxes[0] = selector;

        // Text Box for the artist
        final Label artistLabel = new Label("Artist:");
        artistLabel.setWidth(labelWidth);
        artistLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);  
        artist.setText("[Artist]");
        artist.setWidth("200px");
        userInputBoxes[1] = artist;

        // Text Box for the song title
        final Label titleLabel = new Label("Song Title:");
        titleLabel.setWidth(labelWidth);
        titleLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);     
        title.setText("[Song]");
        title.setWidth("200px");
        userInputBoxes[2] = title;

        // Text Box for the selector's comment
        final Label commentLabel = new Label("Comment (optional):");
        commentLabel.setWidth(labelWidth);
        commentLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);   
        comment.setText("[Comment]");
        comment.setVisibleLines(3);
        comment.setWidth("400px");
        comment.getElement().getStyle().setProperty("minWidth", "400px");
        comment.getElement().getStyle().setProperty("maxWidth", "400px");
        comment.getElement().getStyle().setProperty("minHeight", "75px");
        comment.getElement().getStyle().setProperty("maxHeight", "75px");
        userInputBoxes[3] = comment;

        // Link to the Song of the Day.
        // TODO: This may be replaced with a more elaborate setup in the future (Grooveshark song link, embedded youtube vid, etc.)
        final Label linkLabel = new Label("Link (URL):");
        linkLabel.setWidth(labelWidth);
        linkLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
        link.setText("[Link]");
        link.setWidth("400px");
        userInputBoxes[4] = link;

        //Text Area for SOTD subscribers
		final Label subscribersLabel = new Label("Subscribers:");
		sotdSubscribers.setVisibleLines(10);
		sotdSubscribers.setWidth("400px");
		userInputBoxes[5] = sotdSubscribers;
		
		//Load our existing subscriber list from the data store
		final SotdServiceAsync sotdService = GWT
				.create(SotdService.class);
		sotdService.getSubscriberList( new AsyncCallback<String>(){

			@Override
			public void onFailure(Throwable caught) {
				caught.printStackTrace();
			}

			@Override
			public void onSuccess(String result) {
				Utils.hideLoadingScreen();
				sotdSubscribers.setText(result);
			}
			
		});
        
        int PX_WIDTH_SPACING = 100;
        int PX_HEIGHT_SPACING = 50;

    	// Add a handler to send the name to the server

        selector.addKeyDownHandler(keyDownHandler);
        selector.addFocusHandler(focusHandler);

        artist.addKeyDownHandler(keyDownHandler);
        artist.addFocusHandler(focusHandler);

        title.addKeyDownHandler(keyDownHandler);
        title.addFocusHandler(focusHandler);

        comment.addKeyDownHandler(keyDownHandler);
        comment.addFocusHandler(focusHandler);

        link.addKeyDownHandler(keyDownHandler);
        link.addFocusHandler(focusHandler);
        
        sotdSubscribers.addKeyDownHandler(keyDownHandler);
        sotdSubscribers.addFocusHandler(focusHandler);
        
        
        AbsolutePanel submissionPanel = new AbsolutePanel();
        submissionPanel.add(selectorLabel,	 PX_WIDTH_SPACING * 0,PX_HEIGHT_SPACING * 0);
        submissionPanel.add(selector,		 PX_WIDTH_SPACING * 1,PX_HEIGHT_SPACING * 0);
        submissionPanel.add(artistLabel,	 PX_WIDTH_SPACING * 0,PX_HEIGHT_SPACING * 1);
        submissionPanel.add(artist,			 PX_WIDTH_SPACING * 1,PX_HEIGHT_SPACING * 1);
        submissionPanel.add(titleLabel,		 PX_WIDTH_SPACING * 0,PX_HEIGHT_SPACING * 2);
        submissionPanel.add(title,			 PX_WIDTH_SPACING * 1,PX_HEIGHT_SPACING * 2);
        submissionPanel.add(commentLabel,	 PX_WIDTH_SPACING * 0,PX_HEIGHT_SPACING * 3);
        submissionPanel.add(comment,		 PX_WIDTH_SPACING * 1,PX_HEIGHT_SPACING * 3);
        submissionPanel.add(linkLabel,		 PX_WIDTH_SPACING * 0,PX_HEIGHT_SPACING * 5);
        submissionPanel.add(link,			 PX_WIDTH_SPACING * 1,PX_HEIGHT_SPACING * 5);
        submissionPanel.add(subscribersLabel,PX_WIDTH_SPACING * 0,PX_HEIGHT_SPACING * 6);
        submissionPanel.add(sotdSubscribers, PX_WIDTH_SPACING * 1,PX_HEIGHT_SPACING * 6);
        submissionPanel.setSize("550px", "500px");
        
        initWidget(submissionPanel);
    }
    
    //Public getters to retrieve the text that people type in
    public String getSelector(){
    	return Utils.escapeHtml( selector.getText() );
    }
    
    public String getArtist(){
    	return Utils.escapeHtml( artist.getText() );
    }
    
    public String getTitle(){
    	return Utils.escapeHtml( title.getText() );
    }
    
    public String getComment(){
    	return Utils.escapeHtml( comment.getText() );
    }
    
    public String getLink(){
    	return Utils.escapeHtml( link.getText() );
    }
    
    public String getSubscribers(){
    	return Utils.escapeHtml( sotdSubscribers.getText() );
    }
    
    public String getCompleteMessage(){
    	return "The Song of the Day has been chosen!\n" +
    			"Selector: " + getSelector() + "\n" +
    			"Song: " + getArtist() + " - " + getTitle() + "\n" +
    			"Comment: " + getComment() + "\n" +
    			"Link: " + getLink();
    }
    
    public String getCompleteHTML(){
    	return "The Song of the Day has been chosen!<br>" +
    			"Selector: " + getSelector() + "<br>" +
    			"Song: " + getArtist() + " - " + getTitle() + "<br>" +
    			"Comment: " + getComment()  + "<br>" +
    			"Link: <a href =\"" + getLink()  + "\">" + getLink() +"</a>";
    }
    
    //String userName, String songArtist, String songName, String comment, String link
    public Submission makeSubmission(){
    	Submission submission = new Submission(getSelector(),
    										   getArtist(),
    										   getTitle(),
    										   getComment(),
    										   getLink(),
    										   new Date());
    	submission.escapeSubmission();
    	return submission;
    }    
}
